package com.flnet.spsys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.flnet.spsys.entity.TbWorks;
import com.flnet.spsys.mapper.TbWorksMapper;
import com.flnet.spsys.service.ITbWorksService;
import com.flnet.spsys.utils.ResultJson;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@RestController
@RequestMapping("/works")
public class TbWorksController extends BaseController {
    @Autowired
    ITbWorksService worksService;

    @ApiOperation(value = "作品信息", notes = "作品信息")
    @ApiResponses(
            @ApiResponse(code = 200, message = "获取成功", response = ResultJson.class)
    )

    @PostMapping(value = "/info")
    public Object getWorksInfo() {
        List<TbWorks> worksList = worksService.list();
        for(Iterator itr=worksList.iterator();itr.hasNext();){
            Object str = itr.next();
            System.out.println(str);
        }
        return ResultJson.success(worksList);
//        TbWorks worksById = worksService.findWorksById(id);
    }
    @GetMapping("/delete")
    public boolean deleteWorks(Integer id){

        boolean b = worksService.removeById(1);
        return b;
    }
    @GetMapping("/insert")
    public boolean insertWorks(TbWorks tbWorks){
        tbWorks.setId(6);
        tbWorks.setIndustry("行业1");
        tbWorks.setProNature("性质1");
        tbWorks.setProType("方案类型1");
        tbWorks.setProductNature("产品性质1");
        tbWorks.setPropertyType("物业类型1");
        tbWorks.setCaseTime("2021-03-25");
        boolean save = worksService.save(tbWorks);
        if(save == false){
            System.out.println("添加失败！");
        }else{
            System.out.println("添加成功！");
        }
        return save;
    }
    @GetMapping("/updateWorks")
    public boolean updateWorks(TbWorks tbWorks){
        tbWorks.setId(2);
        tbWorks.setIndustry("行业");
        tbWorks.setProNature("性质");
        tbWorks.setProType("方案类型");
        tbWorks.setProductNature("产品性质");
        tbWorks.setPropertyType("物业类型");
        tbWorks.setCaseTime("2021-03-25");
        boolean b = worksService.updateById(tbWorks);
        return b;
    }

    @ApiOperation(value = "方案模糊查询", notes = "方案查询")
    @ApiResponses(
            @ApiResponse(code = 200, message = "获取成功", response = ResultJson.class)
    )
    @ResponseBody
    @PostMapping(value = "/proinfo")
    @CrossOrigin
    public Object selectPro(TbWorks tbWorks){
        TbWorks byWorksInfo = worksService.findByWorksInfo(tbWorks);
        if(byWorksInfo == null){
            return ResultJson.failure("无方案信息",null);
        }
        return ResultJson.success(byWorksInfo);
    }


    /**
     * 作品详情，连表（用户昵称、下载量。。）
     * @param tbWorks
     * @return
     */
    @ResponseBody
    @PostMapping("/proDetail")
    @CrossOrigin
    public Object showProDetail(TbWorks tbWorks){
        TbWorks worksDetail = worksService.findWorksDetail(tbWorks.getId());
        if(worksDetail == null){
            return ResultJson.failure("无信息",null);
        }
        return ResultJson.success(worksDetail);
    }



}






















